package top.hzy520.role.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.hzy520.annotation.LogAnnotation;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.page.Pagination;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.role.dto.PermissionOutPutDto;
import top.hzy520.role.dto.RoleInputDto;
import top.hzy520.role.entity.Role;
import top.hzy520.role.service.RoleService;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 19:34 2018/9/27
 */
@Controller
public class RoleController {
    @Autowired
    @Qualifier("roleServiceImpl")
    private RoleService roleService;


    @RequestMapping("/role/list")
    @ResponseBody
    public Pagination queryRolePaginationList(int limit,int offset){
        Pagination pagination=new Pagination();
    int count=roleService.selectAllRoleCount();
        List<RoleInputDto> roleList=roleService.selectAllRole(offset,limit);
        pagination.setTotal(count);
         pagination.setRows(roleList);
    return pagination;
    }
    @RequestMapping("/role/authority/{roleId}")
    public String authorityEdit(@PathVariable("roleId") Long roleId,Model model){
        Role role=roleService.selectRoleById(roleId);
        model.addAttribute("role",role);
        return "admin/role/authority";
    }
    @RequestMapping("/permission/list/{roleId}")
    @ResponseBody
    public List<PermissionOutPutDto> treePermissionList(@PathVariable("roleId") Long roleId){
        List<PermissionOutPutDto> permissionOutPutDtoList=roleService.treePermissionList(roleId);
      return permissionOutPutDtoList;
    }
    @RequestMapping(value = "/role/authority/{roleId}",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult authorityEdit(@RequestBody List<PermissionOutPutDto> permissionOutPutDtoList,@PathVariable("roleId") Long roleId){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"修改成功");
        roleService.roleEdit(permissionOutPutDtoList,roleId);
        return apiResult;
    }
    @RequestMapping("/role/edit/{roleId}")
    public String roleEdit(@PathVariable("roleId") Long roleId,Model model){
        Role role=roleService.selectRoleById(roleId);
       List<Role> roleList=roleService.selectRole();
        model.addAttribute("role",role);
        model.addAttribute("roleList",roleList);
        return "admin/role/edit";
    }
    @LogAnnotation(value =Constants.OPERATION_LOG_NAME ,operationNmae = Constants.EDIT_ROLE_LOG_NAME)
    @RequestMapping(value = "/role/edit",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult roleEdit(@RequestBody Role role){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"编辑成功");
        roleService.updateRole(role);
        return apiResult;
    }
    @RequestMapping("/role/add")
    public String roleAdd(Model model){
        List<Role> roleList=roleService.selectRole();
        model.addAttribute("roleList",roleList);
        return "admin/role/add";
    }
    @LogAnnotation(value =Constants.OPERATION_LOG_NAME ,operationNmae = Constants.ADD_ROLE_LOG_NAME)
    @RequestMapping(value = "/role/add",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult roleAdd(@RequestBody Role role){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"添加成功");
        roleService.roleAdd(role);
        return apiResult;
    }
    @RequestMapping(value = "/role/delete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult roleDelete(@RequestBody Role role){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"添加成功");
        return apiResult;
    }
}
